# vscode 安装插件elasticsearch for vscode
# 新xxx.es文件
# API 参考：https://www.elastic.co/guide/cn/elasticsearch/guide/current/_indexing_employee_documents.html
GET _cat/indices

# 创建三个员工
PUT /megacorp/employee/1
{
    "first_name" : "John",
    "last_name" :  "Smith",
    "age" :        25,
    "about" :      "I love to go rock climbing",
    "interests": [ "sports", "music" ]
}
PUT /megacorp/employee/2
{
    "first_name" :  "Jane",
    "last_name" :   "Smith",
    "age" :         32,
    "about" :       "I like to collect rock albums",
    "interests":  [ "music" ]
}

PUT /megacorp/employee/3
{
    "first_name" :  "Douglas",
    "last_name" :   "Fir",
    "age" :         35,
    "about":        "I like to build cabinets",
    "interests":  [ "forestry" ]
}

GET /megacorp/employee/1

GET /megacorp/employee/_search

GET /megacorp/employee/_search?q=last_name:Smith

GET /megacorp/employee/_search
{
    "query": {
        "match": {
            "last_name": "Smith"
        }
    }
}

GET /megacorp/employee/_search
{
    "query": {
        "bool": {
            "must": {
                "match": {
                    "last_name": "smith"
                }
            },
            "filter": {
                "range": {
                    "age": {
                        "gt": 30
                    }
                }
            }
        }
    }
}

# 相关度排名
GET /megacorp/employee/_search
{
    "query": {
        "match": {
            "about": "rock climbing"
        }
    }
}
# 短语搜索
GET /megacorp/employee/_search
{
    "query": {
        "match_phrase": {
            "about": "rock climbing"
        }
    }
}

# 高亮
GET /megacorp/employee/_search
{
    "query": {
        "match_phrase": {
            "about": "rock climbing"
        }
    },
    "highlight": {
        "fields": {
            "about": {}
        }
    }
}

# 聚合：兴趣
GET /megacorp/employee/_search
{
    "aggs": {
        "all_interests": {
            "terms": {
                "field": "interests"
            }
        }
    }
}

GET /megacorp/employee/_search
{
    "query": {
        "match": {
            "last_name": "smith"
        }
    },
    "aggs": {
        "all_interests": {
            "terms": {
                "field": "interests"
            }
        }
    }
}

# 聚合：兴趣（平均年龄）
GET /megacorp/employee/_search
{
    "aggs": {
        "all_interests": {
            "terms": {
                "field": "interests"
            },
            "aggs": {
                "avg_age": {
                    "avg": {
                        "field": "age"
                    }
                }
            }
        }
    }
}

#=========== 新增商品 =============
POST /my_store/products/1
{ "price" : 10, "productID" : "XHDK-A-1293-#fJ3" }
POST /my_store/products/2
{ "price" : 20, "productID" : "KDKE-B-9947-#kL5" }
POST /my_store/products/3
{ "price" : 30, "productID" : "JODL-X-1937-#pV7" }
POST /my_store/products/4
{ "price" : 30, "productID" : "QQPX-R-3956-#aD8" }

#=========== 查询所有 =============
GET /my_store/products/_search
{
    "query": {
        "term": {
            "price": 20
        }
    }
}
# 不计算评份constant_score
GET /my_store/products/_search
{
    "query" : {
        "constant_score" : { 
            "filter" : {
                "term" : { 
                    "price" : 20
                }
            }
        }
    }
}

# 无法获得期望的结果（被拆分成多个更小的 token）
GET /my_store/products/_search
{
    "query": {
        "constant_score": {
            "filter": {
                "term": {
                    "productID": "QQPX-R-3956-#aD8"
                }
            }
        }
    }
}

# 测试分词器
GET /my_store/_analyze
{
    "field": "productID",
    "text": "XHDK-A-1293-#fJ3"
}

# 删除原索引
DELETE /my_store

# 创建新索引（"index": "not_analyzed"）
PUT /my_store 
{
    "mappings": {
        "products": {
            "properties": {
                "productID": {
                    "type": "string",
                    "index": "not_analyzed"
                }
            }
        }
    }
}

# 布尔过滤器：一个 bool 过滤器由三部分组成：
{
   "bool" : {
      "must" :     [AND],
      "should" :   [OR],
      "must_not" : [NOT],
   }
}
# filtered 查询将所有的东西包起来。
GET /my_store/products/_search
{
    "query": {
        "filtered": {
            "filter": {
                "bool": {
                    "should": [
                        {
                            "term": {
                                "price": 20
                            }
                        },
                        {
                            "term": {
                                "productID": "XHDK-A-1293-#fJ3"
                            }
                        }
                    ],
                    "must_not": {
                        "term": {
                            "price": 30
                        }
                    }
                }
            }
        }
    }
}

# 多组合逻辑条件
# SELECT document
# FROM   products
# WHERE  productID      = "KDKE-B-9947-#kL5"
#  OR (     productID = "JODL-X-1937-#pV7"
#       AND price     = 30 )

GET /my_store/products/_search
{
   "query" : {
      "filtered" : {
         "filter" : {
            "bool" : {
              "should" : [
                { "term" : {"productID" : "KDKE-B-9947-#kL5"}}, 
                { "bool" : { 
                  "must" : [
                    { "term" : {"productID" : "JODL-X-1937-#pV7"}}, 
                    { "term" : {"price" : 30}} 
                  ]
                }}
              ]
           }
         }
      }
   }
}

# terms多个值（区别于term）
GET /my_store/products/_search
{
    "query": {
        "constant_score": {
            "filter": {
                "terms": {
                    "price": [
                        20,
                        30
                    ]
                }
            }
        }
    }
}
整个字段完全相等
GET /my_index/my_type/_search
{
    "query": {
        "constant_score" : {
            "filter" : {
                 "bool" : {
                    "must" : [
                        { "term" : { "tags" : "search" } }, 
                        { "term" : { "tag_count" : 1 } } 
                    ]
                }
            }
        }
    }
}

# 范围查询(range 查询同样可以应用在日期字段上：)
GET /my_store/products/_search
{
    "query": {
        "constant_score": {
            "filter": {
                "range": {
                    "price": {
                        "gte": 20,
                        "lt": 40
                    }
                }
            }
        }
    }
}

#过去一小时内的所有文档：
"range" : {
    "timestamp" : {
        "gt" : "now-1h"
    }
}
早于 2014 年 1 月 1 日加 1 月（2014 年 2 月 1 日 零时）
"range" : {
    "timestamp" : {
        "gt" : "2014-01-01 00:00:00",
        "lt" : "2014-01-01 00:00:00||+1M" 
    }
}